package com.mediacloud.app.newsmodule.fragment.baoliao.frag

import android.media.MediaPlayer
import android.view.Surface
import android.view.SurfaceHolder
import com.mediacloud.app.assembly.widget.media.IMediaKernel
import com.mediacloud.app.assembly.widget.media.IMediaPlayerListener
import com.mediacloud.app.assembly.widget.media.IVideoPlayerListener


class AndroidKernel : IMediaKernel {
    val player: MediaPlayer = MediaPlayer()

    override fun setLooping(looping: Boolean) {
        player.isLooping = looping
    }

    override fun setDataSource(url: String?) {
        player.setDataSource(url)
    }

    override fun prepareAsync() {
        player.prepareAsync()
    }

    override fun getCurrentState(): Int {
        return -1
    }

    override fun start() {
        player.start()
    }

    override fun pause() {
        player.pause()
    }

    override fun stop() {
        player.stop()
    }

    override fun release() {
        player.release()
    }

    override fun reset() {
        player.reset()
    }

    override fun seekTo(msec: Long) {
        player.seekTo(msec.toInt())
    }

    override fun getCurrentPosition(): Long {
        return player.currentPosition.toLong()
    }

    override fun getDuration(): Long {
        return player.duration.toLong()
    }

    override fun setDisplay(holder: SurfaceHolder) {
        player.setDisplay(holder)
    }

    override fun setMediaPlayerListener(listener: IMediaPlayerListener) {
        player.setOnPreparedListener { listener.onPrepared() }
        player.setOnSeekCompleteListener { listener.onSeekComplete() }
        player.setOnCompletionListener { listener.onCompletion() }
        player.setOnErrorListener { mp, what, extra -> listener.onError(what, extra) }

        player.setOnBufferingUpdateListener { mp, percent -> listener.onBufferingUpdate(percent) }

    }

    override fun setOnVideoSizeChangedListener(listener: IVideoPlayerListener) {
        player.setOnVideoSizeChangedListener { mp, width, height ->
            val videoWidth: Int = player.videoWidth
            val videoHeight: Int = player.videoHeight
            if (videoWidth != 0 && videoHeight != 0)
                listener.onVideoSizeChanged(videoWidth, videoHeight)
        }
    }

    override fun setSurface(surface: Surface?) {
        player.setSurface(surface)
    }

    override fun setEnableMediaCodec(isEnable: Boolean) {
        //no support
    }

}